package com.project.myxima;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.project.myxima.adapters.IndicatorAdapter;
import com.project.myxima.adapters.LoginViewPagerAdapter;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

public class LoginActivity extends AppCompatActivity {


    private ViewPager mLoginPager;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void initView() {
        //initial indicator and viewPager

        //1. indicator
        MagicIndicator indicator = this.findViewById(R.id.login_indicator);
        indicator.setBackgroundColor(getResources().getColor(R.color.main_color,null));
          //Navigator
        CommonNavigator navigator = new CommonNavigator(this);
        List<String> pagerTitles = Arrays.asList(getResources().getStringArray(R.array.login_titles));
        IndicatorAdapter indicatorAdapter = new IndicatorAdapter(this,mLoginPager,pagerTitles);
        navigator.setAdapter(indicatorAdapter);
        navigator.setAdjustMode(true);

        indicator.setNavigator(navigator);

        //2. viewPager
        FragmentManager manager = getSupportFragmentManager();
        LoginViewPagerAdapter adapter = new LoginViewPagerAdapter(manager);
        mLoginPager = findViewById(R.id.login_view_pager);
        mLoginPager.setAdapter(adapter);

        //bind indicator and viewPager
        ViewPagerHelper.bind(indicator,mLoginPager);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLoginPager = null;
    }

    public void switchFragment(int fragment) {
        mLoginPager.setCurrentItem(fragment);
    }
}
